<template>
  <div class="data-table-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.params" inline>
        <el-form-item class="!mr-4" prop="uid">
          <el-input v-model="state.tableData.param.params.uid"
                    placeholder="请输入订单号" size="default"/>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" highlight-current-row
                style="width: 100%"
                @sort-change="handleSortChange">
        <el-table-column label="订单" prop="uid" show-overflow-tooltip/>
        <el-table-column label="商品名称" prop="commodityName" show-overflow-tooltip/>
        <el-table-column label="应付金额" prop="amount" show-overflow-tooltip/>
        <el-table-column label="支付金额" prop="payAmount" show-overflow-tooltip/>
        <el-table-column label="砍价金额" prop="bargaining" show-overflow-tooltip/>
        <el-table-column label="销售单价" prop="price" show-overflow-tooltip/>
        <el-table-column label="商品数量" prop="quantity" show-overflow-tooltip/>
        <el-table-column label="寄存数量" prop="deposit" show-overflow-tooltip/>
        <el-table-column label="赠送数量" prop="gifts" show-overflow-tooltip/>
        <el-table-column label="商品成本" prop="costAmount" show-overflow-tooltip/>
        <el-table-column label="商品利润" prop="profitAmount" show-overflow-tooltip/>
        <el-table-column label="优惠金额" prop="discountAmount" show-overflow-tooltip/>
        <el-table-column label="提成金额" prop="commissionAmount" show-overflow-tooltip/>
        <el-table-column label="支付时间" prop="payDate" show-overflow-tooltip/>
        <el-table-column label="支付方式" prop="payType" show-overflow-tooltip/>
        <el-table-column label="支付状态" prop="payStatus" show-overflow-tooltip/>
        <el-table-column label="备注" prop="remark"/>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.param.pageNum"
                     v-model:page-size="state.tableData.param.pageSize" :page-sizes="[10, 20, 30]" :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <serverDialog ref="serverDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';
import {saleStoreDetailApi} from "/@/api/sale/commodityDetail";

// 引入组件
const serverDialog = defineAsyncComponent(() => import('/@/views/store/server/category/dialog.vue'));

// 定义变量内容
const serverDialogRef = ref();
const queryFormRef = ref();
const state = reactive<SysUserState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      params: {
        uid: '',
      },
    },
  },
});

const handleSortChange = (data: any) => {
  if (data.order) {
    if (data.order.indexOf('asc') > -1) {
      state.tableData.param.sortOrder = 'asc'
    } else {
      state.tableData.param.sortOrder = 'desc'
    }
    state.tableData.param.sortName = data.prop
  }
  getTableData();
};

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  saleStoreDetailApi()
      .getTableData(state.tableData.param)
      .then((res: any) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
